package com.wy.dzsw.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2020/11/1810:51
 */
@RestController
@RequestMapping("file")
public class FileController {

    @RequestMapping("upload")
    public Map<String,String> uplaod(MultipartFile file){
        Map<String,String> map = new HashMap<>();
        //防止用户上传文件名称重复；用uuid代替原理的名字
        //获取文件真实名称
        String name= file.getOriginalFilename();
        //获取名称后缀
        String affix = name.substring(name.lastIndexOf("."));
        String savename=UUID.randomUUID().toString()+affix;

        //保存到本地
        savelocal(file, map, savename);
        //savefastdfs(multipartFile, map, affix.substring(1));
        return map;
    }


    private void savelocal(MultipartFile multipartFile, Map<String, String> map, String savename) {
        try {
            multipartFile.transferTo(new File("D:\\uploads\\"+savename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("imgsrc","/uploads/"+savename);
    }
}
